<!--
公文收藏
-->
<template>
	<view>
		<van-search
		  v-model="title"
		  placeholder="请输入关键字"
		  @input="onSearch"
		>
		</van-search>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-empty description="暂无数据" v-show="emptyData"/>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载" :immediate-check="immediateCheck">
        <van-swipe-cell  v-for="(item,index) in list" :right-width="90">
          <van-cell center :title="item.gwTitle" :label="'公文类型：'+item.gwTypeName" :value="item.createTime.substring(0,10)"
                    @click="toDetail(item.gwId)" value-class="text-sm">
          </van-cell>
          <template #right>
            <van-checkbox :name="item.bizId" :key="item.bizId" @click.native="cancelcCollection(item,index)">
              <text>{{ collectionState }}</text>
              <template #icon="props">
                <img class="img-icon" :src="activeIcon" />
              </template>
            </van-checkbox>
          </template>
        </van-swipe-cell>
      </van-list>
    </van-pull-refresh>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        collectionState:'取消收藏',
        activeIcon: require('@/static/imgs/icon/icon-collection-active.png'),
        title:'',//搜索关键字
        emptyData:false,//是否显示暂无数据图片
        list: [],
        error: false,     // 错误状态
        loading: false,   // 加载状态
        finished: false,  // 是否加载
        refreshing: false,// 是否刷新
        page: 1,   		// 页数
        rows: 10, 		// 条数
        immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
			};
		},
		methods: {
      onSearch(){
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.onRefresh()
        },300)

      },
      //取消收藏
      cancelcCollection(item,index){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/collection/cancelcollection`),
          method: 'delete',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
            'gwId':item.gwId,
          })
        }).then(({data}) => {
          //{"code":200,"data":"取消收藏成功!","message":null}
          if(data.code == 200){
            this.$toast('取消收藏成功')
            this.onRefresh()
          }else{
            this.$toast(data.data.message)
          }
        })
      },
      // 下拉刷新
      onRefresh() {
        // 先清空数据
        this.list = []
        // 将没有更多的状态改成false
        this.finished = false
        // 默认加载第一页
        this.page = 1
        this.queryList()
        this.refreshing = false
      },
      load() {
        // 页数设置
        if(this.list.length === 0){
          this.page = 1
        } else {
          this.page++
        }
        // 将下拉刷新停止
        this.refreshing = false;
        setTimeout(() => {
          this.queryList()
        }, 500)
      },

      //查询公文
      queryList(){
        this.loading = true
        // 获取要显示的按钮
        this.$http({
          // url: this.$http.adornUrl(`/caac/oa/common/collectionlist/getcollectionsByDirId/root`),
          url: this.$http.adornUrl(`/caac/oa/common/collectionlist/queryCollection`),
          method: 'get',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,//固定值
            'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
            'title':this.title,
            'page':this.page,
            'rows':this.rows
          })
        }).then(({data}) => {
          if(data.code==200){
            if(this.util.isNotEmpty(data.data)) {
              this.loading = false;
              if(this.page == 1) {
                this.list = data.data.rows
              } else {
                data.data.rows.forEach( t => this.list.push(t))
              }
              if(this.list.length==0){
                this.emptyData = true
              }else{
                this.emptyData = false
              }
              // 判断当前页是否为最后一页
              if((this.page*this.rows) > data.data.total) {
                this.finished = true
              }
            } else {
              this.finished = true
            }
          }else{
            this.error = true
            console.log("出错data: " + JSON.stringify(data));
          }
        }).catch((err) => {
          this.error = true
        }).finally(() => {
          // 关闭加载状态
          this.loading = false
        })
      },

      //跳转详情code:发文类型，proInstId
      toDetail(proInstId){
        var from = "search"//如果是从查询里进详情，则不显示操作按钮
        // 会签件--hqj，发文（函）--glj_fw，呈批件--glj_cpj，发电--fd_hbglj，发电抄阅--hbgljfd，收文--glj_sw
        var _this = this
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
          method: 'get',
          params: this.$http.adornParams({
            'proInstId': proInstId,//发文详情返回的proInstId
            'systemKey': this.cache.get('userInfo').systemKey,//固定值
            'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          })
        }).then(({data}) => {
          if(data.code==200){
            _this.configCode = data.data.configCode
            console.log("公文类型==>"+_this.configCode)
            if(_this.configCode != ''){
              this.common.toJgj(_this.configCode,proInstId,'search')
            }
          }else{
            this.$toast(data.message)
          }

        })

        // this.openwfneededdata(extendIdForRelate,proInstId)


      },

		},
	};
</script>

<style scoped>
	/deep/ .van-search__content {
		padding-left: 0;
	}
	/deep/ .van-cell {
		padding: 16rpx 30rpx;
	}
	/deep/ .van-dropdown-menu__bar {
    box-shadow: none;
	}
  /deep/ .van-dropdown-menu__bar--opened {
    z-index: 10;
  }
  /deep/ .van-search__action {
    margin: 0 8px;
    padding: 0 0px;
  }
  /*收藏样式*/
  /deep/  .van-swipe-cell__right{
    display: flex;
    align-items: center;
  }
  .img-icon {
    height: 40rpx;
  }
  /*收藏样式*/
</style>
